package org.business.accounting.pages;

import java.util.Collection;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Session;
import org.business.accounting.beans.BasicOrderBean;
import org.business.accounting.beans.CartBean;
import org.business.accounting.beans.LogOutHelperBean;
import org.business.accounting.components.BasicDisplay;
import org.business.accounting.interfaces.BasicOrder;
import org.business.accounting.interfaces.Cart;
import org.business.accounting.interfaces.ComputerOrder;
import org.business.accounting.interfaces.LogOutHelper;
import org.business.accounting.interfaces.User;
import org.business.accounting.service.OrderDAO;
import org.business.accounting.service.UserDAO;

@SuppressWarnings("unused")
public class Purchase {

	@ApplicationState
	@Property
	private User user;

	@Property
	private boolean userExists;

	@ApplicationState
	private Cart cart;

	private boolean cartExists;
	
	@Inject
	private OrderDAO orderDAO;

	private BasicOrder basicOrder;

	@Property
	private boolean basicOrderExists;

	@Persist
	@Property
	private LogOutHelper logOutHelper;

	//
	// Purchase Item
	//

	//
	// Geters and Seters
	//

	public boolean isAdmin() {
		if (userExists) {
			return user.isAdmin();
		}
		return false;
	}

	public BasicOrder getBasicOrder() {
		return basicOrder;
	}

	public void setBasicOrder(BasicOrder basicOrder) {
		this.basicOrder = basicOrder;
	}

	// NEW
	public Cart getCart() {
		if (cartExists) {
			return cart;
		} else {
			return new CartBean();
		}
	}

	public void setCart(Cart cart) {
		this.cart = cart;
	}

	//
	// Event handlers
	//
	
	void onActivate(BasicOrder basicOrder) {
		this.basicOrder = basicOrder;
	}
	
	BasicOrder onPassivate() {
		return this.basicOrder;
	}
	
	@SetupRender
	void onSetupRender() {
		if (!cartExists) {
			cart = new CartBean();
		}

		logOutHelper = new LogOutHelperBean();
		logOutHelper.setCart(cart);
		if (userExists) {
			logOutHelper.setUser(user);
		}
	}

}
